<?php
class ArticlesController extends AppController {

	var $name = 'Articles';

	function _index($category_id = null, $status = 1, $expired = null, $limit = 5) {
		if(!$category_id){
			$this->redirect('/');
		}
		$expired = !empty($expired)?$expired:date('Y-m-d H:i:s');
		$this->Article->recursive = 0;
		$this->paginate = array('conditions' => array('Article.category_id ' => $category_id, 'Article.status' => $status, 'Article.expired >= ' => $expired), 'order' => 'Article.created DESC', 'limit' => $limit);
		return $this->paginate();
	}
	//专注类文章
	function contact(){
		$this->layout = 'shinrun.view';
		$this->helpers = array('Html', 'Session', 'Aider');
		$this->getTags();
		$this->getCategories();
		$this->set('articles', $this->_index(array(4)));
		$this->set('title_for_layout', '联络');
		$this->set('keywords', '联络欣然');
		$this->set('description', '欣然的联系方式');
	}
	//分类 列表
	function lists($category_id = null){
		$this->layout = 'shinrun.view';
		$this->helpers = array('Html', 'Session', 'Text', 'Aider');
		
		$this->getTags();
		$this->getCategories();
		if(!$category_id){
			$this->redirect('/');
		}
		$category = $this->Article->Category->read(null, $category_id);
		//get aritles
		$this->set('articles', $this->_index($category_id));
		$this->set('category', $category);
		$this->set('title_for_layout', $category['Category']['name'] . ' 列表');
		$this->set('keywords', $category['Category']['name'] . '文章列表');
		$this->set('description', '欣然网站的文章列表');
	}
	
	function search(){
		$this->layout = 'shinrun.view';
		$this->helpers = array('Html', 'Session', 'Text', 'Aider');
		
		$this->getTags();
		$this->getCategories();
		$kw 	  = isset($this->params['named']['kw']) ? trim($this->params['named']['kw']) : '';
		$keywords = isset($_POST['search_kw']) ? htmlentities(trim($_POST['search_kw']), ENT_QUOTES | ENT_IGNORE, "UTF-8") : $kw;
		$expired  = !empty($expired)?$expired:date('Y-m-d H:i:s');
		if(empty($keywords)){
			$this->redirect('/');
		}
		$this->Article->recursive = 0;
		$this->paginate = array('conditions' => array('OR' => array(array('Article.title LIKE' => "%" . $keywords . "%"), array('Article.contents LIKE' => "%" . $keywords . "%")), 'Article.status' => 1, 'Article.expired >= ' => $expired), 'order' => 'Article.created DESC', 'limit' => 5);
		$this->set('kw', $keywords);
		$this->set('title_for_layout', '搜索');
		$this->set('keywords', '网站搜索');
		$this->set('description', '欣然文章搜索');
		$this->set('articles',$this->paginate());
	}
	function view($id = null) {
		$this->layout = 'shinrun.view';
		$this->helpers = array('Html', 'Session', 'Text', 'Aider');
		if (!$id) {
			$this->Session->setFlash(__('Invalid article', true));
			$this->redirect(array('action' => 'lists'));
		}
		$article = $this->Article->read(null, $id);
		if(!$article['Article']['status']){
			$this->Session->setFlash(__('This article is hidden, if you need, please contact yunter@shinrun.com.', true));
			$this->redirect(array('action' => 'lists'));
		}
		$this->getTags();
		$this->getCategories();
		$this->set('article', $article);
		$this->set('title_for_layout', '欣然－' . $article['Article']['title']);
		$this->set('keywords', $article['Article']['tags']);
		$this->set('description', '欣然IT－' . $article['Article']['title']);
	}

	function admin_index() {
		$this->_check();
		$this->Article->recursive = 0;
		$this->paginate = array('order' => 'Article.created DESC', 'limit' => 10);
		$this->set('articles', $this->paginate());
	}

	function admin_view($id = null) {
		$this->_check();
		if (!$id) {
			$this->Session->setFlash(__('Invalid article', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('article', $this->Article->read(null, $id));
	}

	function admin_add() {
		$this->_check();
		$this->helpers = array('Html', 'Form', 'Session', 'Cksource');
		if (!empty($this->data)) {
			$this->Article->create();
			//if os linux
			$this->data['Article']['ads'] = !empty($this->data['Article']['ads']) ? str_replace('/app/webroot/', '/', $this->data['Article']['ads']) : $this->data['Article']['ads'];
			$this->data['Article']['contents'] = !empty($this->data['Article']['contents']) ? str_replace('/app/webroot/', '/', $this->data['Article']['contents']) : $this->data['Article']['contents'];
			if ($this->Article->save($this->data)) {
				$this->Session->setFlash(__('The article has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The article could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Article->User->find('list');
		$categories = $this->Article->Category->generatetreelist(null, null, null, '››');
		$this->set(compact('users', 'categories'));
	}

	function admin_edit($id = null) {
		$this->_check();
		$this->helpers = array('Html', 'Form', 'Session', 'Cksource');
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid article', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			//if os linux
			$this->data['Article']['ads'] = !empty($this->data['Article']['ads']) ? str_replace('/app/webroot/', '/', $this->data['Article']['ads']) : $this->data['Article']['ads'];
			$this->data['Article']['contents'] = !empty($this->data['Article']['contents']) ? str_replace('/app/webroot/', '/', $this->data['Article']['contents']) : $this->data['Article']['contents'];
			if ($this->Article->save($this->data)) {
				$this->Session->setFlash(__('The article has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The article could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Article->read(null, $id);
		}
		$users = $this->Article->User->find('list');
		$categories = $this->Article->Category->generatetreelist(null, null, null, '››');
		$this->set(compact('users', 'categories'));
	}

	function admin_delete($id = null) {
		$this->_check();
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for article', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Article->delete($id)) {
			$this->Session->setFlash(__('Article deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Article was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>